package com.huangdada.hfweather.task;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HFWeatherCodeMapping {

	private static final Logger log = LoggerFactory.getLogger(HFWeatherCodeMapping.class);
	private static final Properties props = new Properties();
	private static final String DEFAULT_MAPPING_FILE = "hf_weather_code_mapping.properties";
	
	static {
		InputStream in = null;
		try {
			in = HFWeatherCodeMapping.class.getClassLoader().getResourceAsStream(DEFAULT_MAPPING_FILE);
			props.load(new InputStreamReader(in, "UTF-8"));
		} catch (Exception e) {
			log.error("加载配置文件:hf_weather_code_mapping.properties读取配置出错!", e);
			throw new RuntimeException("加载配置文件:hf_weather_code_mapping.properties读取配置出错!",e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static String getWeatherCode(String key, String defaultValue) {
		String value = props.getProperty(key);
		return StringUtils.isEmpty(value) ? defaultValue : value;
	}

	public static String getWeatherCode(String key) {
		return getWeatherCode(key, null);
	}

	public static int getWeatherCode(int key, int defaultValue) {
		String value = getWeatherCode(String.valueOf(key));
		return StringUtils.isEmpty(value) ? defaultValue : Integer.valueOf(value);
	}
	
	public static int getWeatherCode(int key) {
		String value = getWeatherCode(String.valueOf(key));
		return StringUtils.isEmpty(value) ? -1 : Integer.valueOf(value);
	}
}
